﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDSP.Util;
using System.Data;
using LDSP.BRL;
using BYFW.SFL;

namespace ImplementationInitialization
{
    public class ChemicalImport
    {
        IDictionary<string, Chemical> _chemicalDic = InitializeChemicalDic();
        IDictionary<string, ChemicalCatalog> _chemicalCatalogDic = InitialChemicalCatalogDic();

        public ImportChemicalResult ImportResult { get; set; }

        private static IDictionary<string, ChemicalCatalog> InitialChemicalCatalogDic()
        {
            IDictionary<string, ChemicalCatalog> chemicalDic = new Dictionary<string, ChemicalCatalog>();
            List<ChemicalCatalog> chemicals = new ChemicalCatalog().GetSpecialList<ChemicalCatalog>(null, null);
            foreach (ChemicalCatalog chemical in chemicals)
            {
                if (!chemicalDic.ContainsKey(chemical.Name))
                {
                    chemicalDic.Add(chemical.Name, chemical);
                }
            }

            return chemicalDic;
        }

        private static IDictionary<string, Chemical> InitializeChemicalDic()
        {
            IDictionary<string, Chemical> chemicalDic = new Dictionary<string, Chemical>();
            List<Chemical> chemicals = new Chemical().GetSpecialList<Chemical>(null, null);
            foreach (Chemical chemical in chemicals)
            {
                if (!chemicalDic.ContainsKey(chemical.KEY))
                {
                    chemicalDic.Add(chemical.KEY, chemical);
                }
            }

            return chemicalDic;
        }

        public void Run(string filename)
        {
            ExcelClass cls = new ExcelClass(filename);
            ImportResult = new ImportChemicalResult();

            ImportResult.AddItemResult(filename, "", "", "开始导入文件", true);

            try
            {
                for (int i = 0; i < cls.Sheets.Count; i++)
                {
                    DataTable dt = cls.ToDataTable(i);
                    string sheetName = cls.Sheets[i].Name;
                    if (sheetName.Trim() != "" && _chemicalCatalogDic.ContainsKey(sheetName.Trim()))
                    {
                        ImportResult.AddItemResult(filename, sheetName, "", "开始导入sheet", true);
                        ImportSheet(filename, sheetName, dt);
                        ImportResult.AddItemResult(filename, sheetName, "", "结束导入sheet", true);
                    }
                    else
                    {
                        ImportResult.AddItemResult(filename, sheetName, "", "sheet名不是系统已有的名录名，该sheet被跳过", true);
                    }
                }
            }
            catch (Exception ex)
            {
                ImportResult.AddItemResult(filename, "上一个", "", ex.Message, false);
            }

            ImportResult.AddItemResult(filename, "", "", "结束导入文件", true);
        }

        private void ImportSheet(string excelFileName, string sheetName, DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    Chemical ett = new Chemical();
                    ett.Name = row.ItemArray.Length > 0 && row[1] != null ? row[1].ToString() : string.Empty;//名称
                    ett.Alias = row[2] != null ? row[2].ToString() : string.Empty;//别名
                    ett.Catalog = _chemicalCatalogDic[sheetName].ID.ToString();//目录
                    try
                    {//名称,别名,目录三者组成了KEY.
                        if (_chemicalDic.ContainsKey(ett.KEY))
                        {
                            ett = _chemicalDic[ett.KEY];
                            SetChemical(ett, row);
                            ett.Update();
                            ImportResult.AddItemResult(excelFileName, sheetName, ett.Name, "更新", true);
                        }
                        else
                        {
                            //若不存在于系统，则add.
                            SetChemical(ett, row);
                            ett.Add();
                            ImportResult.AddItemResult(excelFileName, sheetName, ett.Name, "添加", true);
                        }
                    }
                    catch (Exception ex)
                    {
                        ImportResult.AddItemResult(excelFileName, sheetName, ett.Name, ex.Message, false);
                    }
                }
            }
        }

        private void SetChemical(Chemical chemical, DataRow row)
        {
            int i_catalogOrder = 0;
            int i_alias = 2;
            int i_cas = 3;
            int i_un = 4;
            int i_chemicalType = 5;
            int i_danger_number = 6;
            int i_customs_number = 7;
            int i_danger_type = 8;
            int i_package_type = 9;
            int i_unit = 10;
            int i_limitedScope = 11;
            int i_transportationMethod = 12;
            int i_englishName = 13;
            int i_typeCatalog = 14;
            int i_secondaryDangerous = 15;
            int i_exceptionNumber = 16;
            int i_limitationNumber = 17;

            //目录序号
            chemical.CatalogOrder = row[i_catalogOrder] != null ? row[i_catalogOrder].ToString() : string.Empty;

            //CAS号
            chemical.CAS = row[i_cas] != null ? row[i_cas].ToString() : string.Empty;

            //UN号
            chemical.UN = row[i_un] != null ? row[i_un].ToString() : string.Empty;

            //化学品类型
            chemical.ChemicalType = row[i_chemicalType] != null ? row[i_chemicalType].ToString() : string.Empty;

            //危险货物编号
            chemical.DangerNumber = row[i_danger_number] != null ? row[i_danger_number].ToString() : string.Empty;

            //海关商品编号
            chemical.GoodsNumber = row[i_customs_number] != null ? row[i_customs_number].ToString() : string.Empty;

            //主要燃爆危险性分类
            chemical.DangerType = row[i_danger_type] != null ? row[i_danger_type].ToString() : string.Empty;

            //包装类别
            chemical.PackageType = row[i_package_type] != null ? row[i_package_type].ToString() : string.Empty;

            //受限范围
            chemical.LimitedScope = row[i_limitedScope] != null ? row[i_limitedScope].ToString() : string.Empty;

            //计量单位
            chemical.Unit = row[i_unit] != null ? row[i_unit].ToString() : string.Empty;

            //运输方式
            chemical.TransportMethod = row[i_transportationMethod] != null ? row[i_transportationMethod].ToString() : string.Empty;

            //英文名称
            chemical.EnglishName = row[i_englishName] != null ? row[i_englishName].ToString() : string.Empty;

            //类别或项目
            chemical.TypeCatalog = row[i_typeCatalog] != null ? row[i_typeCatalog].ToString() : string.Empty;

            //次要危险性
            chemical.SecondaryDangerous = row[i_secondaryDangerous] != null ? row[i_secondaryDangerous].ToString() : string.Empty;

            //例外数量
            chemical.ExceptionNumber = row[i_exceptionNumber] != null ? row[i_exceptionNumber].ToString() : string.Empty;

            //限制数量
            chemical.LimitationNumber = row[i_limitationNumber] != null ? row[i_limitationNumber].ToString() : string.Empty;
        }
    }
}
